package com.my.geojson.util.excel;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.AttributeDescriptor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 读取shp数据，并将属性写入Excel中
 * 通过POI导出数据到Excel
 */
public class Shp2Excel {

    public static void main(String[] args) throws Exception {
        exportAttr2Excel("D:\\tmp\\gis\\map2.shp",
                "D:\\tmp\\gis\\writeTest3.xlsx");
    }

    public static void exportAttr2Excel(String mcPath_Shp,String mcPath_Excel) throws Exception {
        int lpCounter = 0;
        List<List<Object>> listRows = new ArrayList<List<Object>>();//存储整个数据表
        //获取shp原始数据
        SimpleFeatureCollection lpFeaColl_Source =
                ShpfileReader.getShpfileData(mcPath_Shp);
        //获取shp的属性表结构
        List<ShpFieldInfo> fieldInfoList = new ArrayList<ShpFieldInfo>();
        List<AttributeDescriptor> attrList = lpFeaColl_Source.getSchema().getAttributeDescriptors();
        for (AttributeDescriptor perAttr : attrList) {
            ShpFieldInfo perField = new ShpFieldInfo();
            perField.setFieldName(perAttr.getLocalName());
            perField.setFieldType(perAttr.getType().getBinding());
            fieldInfoList.add(perField);
        }
        //存储列标题
        List<Object> listColumnName = new ArrayList<Object>();//存储每一行的数据集
        for (ShpFieldInfo perField : fieldInfoList) {
            listColumnName.add(perField.getFieldName());
        }
        listRows.add(listColumnName);
        //开始构造属性表结果集
        SimpleFeatureIterator iteratorSourceShp = lpFeaColl_Source.features();
        while (iteratorSourceShp.hasNext()) {
            lpCounter++;
            List<Object> listColumnValue = new ArrayList<Object>();//存储每一行的数据集
            System.out.println("开始读取第" + lpCounter + "个要素");
            SimpleFeature ltFea_Shp = iteratorSourceShp.next();
            //下面这种方式是按照原有顺序获取属性，如果需要单独获取则使用'getAttribute("列名称")'方式
            Iterator<Property> lpIterator_Property = ltFea_Shp.getProperties().iterator();
            while (lpIterator_Property.hasNext()) {
                Property ltProperty = lpIterator_Property.next();
                //跳过几何字段
                if (ltProperty.getName().toString().equalsIgnoreCase("the_geom")) {
                    continue;
                }
                //完全按照原有的顺序
                listColumnValue.add(ltProperty.getValue());
            }
            //把当前行的数据添加到结果集中
            listRows.add(listColumnValue);
            if (lpCounter > 10) {
                break;
            }
        }
        iteratorSourceShp.close();
        //导出结果集到Excel中
//        String lpPath_Excel = "E:\\9.Tests\\shpAttr2Excel.xlsx";
        try {
//            File f = new File(mcPath_Excel);
//            File tempFile = FileUtil.createTempFile(
//                    "shpAttr2Excel", ".xlsx",
//                    new File("D:\\tmp\\gis"), true);
            //POIException: EmptyFileException
//            ExcelWriter writer = ExcelUtil.getBigWriter(tempFile);
//            ExcelWriter writer = ExcelUtil.getBigWriter(mcPath_Excel);
//            writer.write(listRows, true);
//            writer.close();
            //通过工具类创建writer
            ExcelWriter writer = ExcelUtil.getBigWriter(mcPath_Excel);
            //通过构造方法创建writer
            //ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");
            //跳过当前行，既第一行，非必须，在此演示用
            writer.passCurrentRow();
            //合并单元格后的标题行，使用默认标题样式
            writer.merge(listRows.size() - 1, "测试标题");
            //一次性写出内容，强制输出标题
            writer.write(listRows, true);
            //关闭writer，释放内存
            writer.close();
//            f.createNewFile();
//            ExcelUtility excelOpra = new ExcelUtility(lpPath_Excel);
//            ExcelUtility excelOpra = new ExcelUtility(mcPath_Excel);
            //两个索引都是从0开始，如果已经存在表头，那么跳过表头行即可
//            excelOpra.writeDatas(0, 1, listRows);
//        } catch (FileNotFoundException error1) {
//            error1.printStackTrace();
        } catch (Exception error) {
            error.printStackTrace();
        }
        System.out.println("数据导出结束,End");
    }
}
